package entity

import (
	"fmt"
	"strconv"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// COMP_LCTZ 理财投资导入
type CompLctz struct {
	ID    int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	NY    int       `gorm:"column:NY"`
	CUID  int       `gorm:"column:CUID"`
	ZQJC  string    `gorm:"column:ZQJC"`
	FXR   string    `gorm:"column:FXR"`
	ACBL  float32   `gorm:"column:ACBL"`
	QZ    string    `gorm:"column:QZ"`
	FXRQ  time.Time `gorm:"column:FXRQ"`
	QXR   time.Time `gorm:"column:QXR"`
	DQR   time.Time `gorm:"column:DQR"`
	XQDQR time.Time `gorm:"column:XQDQR"`
	DRJG  string    `gorm:"column:DRJG"`

	EMID1 int     `gorm:"column:EMID1"`
	EMNO1 string  `gorm:"column:EMNO1"`
	ZB1   float32 `gorm:"column:ZB1"`
	EMID2 int     `gorm:"column:EMID2"`
	EMNO2 string  `gorm:"column:EMNO2"`
	ZB2   float32 `gorm:"column:ZB2"`
	EMID3 int     `gorm:"column:EMID3"`
	EMNO3 string  `gorm:"column:EMNO3"`
	ZB3   float32 `gorm:"column:ZB3"`
	EMID4 int     `gorm:"column:EMID4"`
	EMNO4 string  `gorm:"column:EMNO4"`
	ZB4   float32 `gorm:"column:ZB4"`
	EMID5 int     `gorm:"column:EMID5"`
	EMNO5 string  `gorm:"column:EMNO5"`
	ZB5   float32 `gorm:"column:ZB5"`
	DTID  int     `gorm:"column:DTID"`
}

// TableName 理财投资导入
func (CompLctz) TableName() string {
	return "T_COMP_LCTZ"
}

// BeforeCreate
func (i *CompLctz) BeforeCreate(tx *gorm.DB) (err error) {
	count := 0
	_ = base.CHDB.Raw("select count(1) from T_COMP_CLIE where CUNM=?", i.FXR).Row().Scan(&count)
	if count > 0 {
		_ = base.CHDB.Raw("select CUID from T_COMP_CLIE where CUNM=?", i.FXR).Row().Scan(&i.CUID)
		i.DRJG = "导入成功！"
	}

	if i.CUID == 0 && i.EMNO1 == "" {
		i.EMID1 = 0
		i.ZB1 = 100
		i.EMNO1 = "G36000"
		i.DRJG = "客户在系统中不存在，且未指定客户经理，将自动分配到分行公共！"
	}

	if i.EMNO1 != "" {
		sum := i.ZB1 + i.ZB2 + i.ZB3 + i.ZB4 + i.ZB5
		if sum != 100 {
			return fmt.Errorf("占比之和必须为100！")
		}
		var emid1, emid2, emid3, emid4, emid5 int
		var emnm1, emnm2, emnm3, emnm4, emnm5 string

		count := 0
		sqlExYg := "select count(1) from T_SysUser where SysUserName=?"
		_ = tx.Raw(sqlExYg, i.EMNO1).Row().Scan(&count)
		if count > 1 {
			return fmt.Errorf("员工1:%s在系统中存在多个名字，请填入工号！", i.EMNO1)
		}

		sqlEx := "select ID,SysUserName from T_SysUser where SysUserID=? or SysUserName=?"
		row := tx.Raw(sqlEx, i.EMNO1, i.EMNO1).Row()
		row.Scan(&emid1, &emnm1)
		if emid1 == 0 && emnm1 != "分行公共" {
			return fmt.Errorf("员工号1:%s在系统中没有找到！", i.EMNO1)
		}
		i.EMID1 = emid1
		if i.ZB2 > 0 {
			_ = tx.Raw(sqlExYg, i.EMNO2).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工2:%s在系统中存在多个名字，请填入工号！", i.EMNO2)
			}
			row = tx.Raw(sqlEx, i.EMNO2, i.EMNO2).Row()
			row.Scan(&emid2, &emnm2)
			if emid2 == 0 && emnm2 != "分行公共" {
				return fmt.Errorf("员工号2:%s在系统中没有找到！", i.EMNO2)
			}
			i.EMID2 = emid2
		}
		if i.ZB3 > 0 {
			_ = tx.Raw(sqlExYg, i.EMNO3).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工3:%s在系统中存在多个名字，请填入工号！", i.EMNO3)
			}
			row = tx.Raw(sqlEx, i.EMNO3, i.EMNO3).Row()
			row.Scan(&emid3, &emnm3)
			if emid3 == 0 && emnm3 != "分行公共" {
				return fmt.Errorf("员工号3:%s在系统中没有找到！", i.EMNO3)
			}
			i.EMID3 = emid3
		}
		if i.ZB4 > 0 {
			_ = tx.Raw(sqlExYg, i.EMNO4).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工4:%s在系统中存在多个名字，请填入工号！", i.EMNO4)
			}
			row = tx.Raw(sqlEx, i.EMNO4, i.EMNO4).Row()
			row.Scan(&emid4, &emnm4)
			if emid4 == 0 && emnm4 != "分行公共" {
				return fmt.Errorf("员工号4:%s在系统中没有找到！", i.EMNO4)
			}
			i.EMID4 = emid4
		}
		if i.ZB5 > 0 {
			_ = tx.Raw(sqlExYg, i.EMNO5).Row().Scan(&count)
			if count > 1 {
				return fmt.Errorf("员工5:%s在系统中存在多个名字，请填入工号！", i.EMNO5)
			}
			row = tx.Raw(sqlEx, i.EMNO5, i.EMNO5).Row()
			row.Scan(&emid5, &emnm5)
			if emid5 == 0 && emnm5 != "分行公共" {
				return fmt.Errorf("员工号5:%s在系统中没有找到！", i.EMNO5)
			}
			i.EMID5 = emid5
		}
	}

	if i.DRJG == "" {
		i.DRJG = "导入成功！"
	}

	dtid := strconv.Itoa(i.NY)
	dtid = dtid[:4] + "-" + dtid[4:6] + "-01"
	err = tx.Raw("SELECT REPLACE (LAST_DAY(STR_TO_DATE(?, '%Y-%m-%d')),'-','')as dtid;", dtid).Row().Scan(&i.DTID)

	return
}

// 返回结构体
func CompLctzEF() base.EntityFunc {
	return func() interface{} {
		entity := CompLctz{}
		return &entity
	}
}

// 返回结构体数组
func CompLctzESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []CompLctz
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("CompLctz", base.NewDefaultDao(CompLctzEF(), CompLctzESF(), nil, "my"))
}
